package com.walleipt.demo.aop.customAopSimple.logger;

import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.beans.BeansException;

import com.walleipt.demo.aop.customAopSimple.annotation.BeforeLoggerProxy;

public class BeforeLoggerAutoProxyCreator extends AbstractAutoProxyCreator {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1865822811049017665L;
	
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		
		this.setInterceptorNames(new String[]{"beforeLoggerInterceptor"}) ;
		
		return super.postProcessAfterInitialization(bean, beanName);
	}
	
	@Override
	protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass,
			String beanName, TargetSource customTargetSource)
			throws BeansException {
			 
		// 如果result返回null则不执行动态代理创建目标对象；反之使用动态代理创建
		Object[] result = beanClass.getAnnotation(BeforeLoggerProxy.class)!=null?PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS:null;
		
		return result ;
	}

}
